package com.dgut.coal.controller;

import com.dgut.coal.entity.Auditrecord;
import com.dgut.coal.entity.Supply;
import com.dgut.coal.service.AuditrecordService;
import com.dgut.coal.service.SupplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

@Controller
public class SupplyController {
    @Autowired
    SupplyService ss;

    @Autowired
    AuditrecordService as;

    @RequestMapping("/findByname")
    public String findallsupply(String supply_name,Map<String, Object> map){
        Supply sp=ss.findByName(supply_name);
        map.put("sp",sp);
        List<Auditrecord> ad=as.find();
        map.put("auditrecord",ad);
        return "supply-view";
    }

    //供应商审核
    @RequestMapping("/findByname2")
    public String findallsupply2(String supply_name,Map<String, Object> map){
        Supply sp=ss.findByName(supply_name);
        map.put("sp",sp);
        List<Auditrecord> ad=as.find();
        map.put("auditrecord",ad);
        return "supply-examine-view";
    }

    @RequestMapping("/findsimple")
    public String findsimplesupply(Map<String, Object> map,Integer supply_id,String supply_name, @RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Supply> sp=ss.findsimplesupply(supply_id,supply_name);
        PageInfo pageInfo = new PageInfo<>(sp,5);
        List keywords = Arrays.asList(supply_id,supply_name);
        map.put("pageInfo",pageInfo);
        map.put("keywords",keywords);
        return "supply-list";
    }

    //供应商审核查找
    @RequestMapping("/findsimple2")
    public String findsimplesupply2(Map<String, Object> map,Integer supply_id,String supply_name, @RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Supply> sp=ss.findsimplesupply(supply_id,supply_name);
        PageInfo pageInfo = new PageInfo<>(sp,5);
        List keywords = Arrays.asList(supply_id,supply_name);
        map.put("pageInfo",pageInfo);
        map.put("keywords",keywords);
        return "supply-examine-list";
    }

    @RequestMapping("/blacklist")
    @ResponseBody
    public int putblacklist(String supply_name,String blacklist_msg,Map<String, Object> map){
        int row= ss.putblacklist(supply_name,blacklist_msg);
        return row;
    }
    @RequestMapping("to_blacklist")
    public String to_blacklist(String supply_name,Map<String,Object> map){
        map.put("supply_name",supply_name);
        return "blacklist";
    }

    @RequestMapping("to_supplylist")
    public String to_supplyview(String supply_name,Map<String,Object> map){
        return "redirect:findsimple";
    }

    @RequestMapping("to_supplyexaminelist")
    public String to_supplyexaminelist(){
        return "redirect:findsimple2";
    }

    @RequestMapping("to_changecategory")
    public String to_changecategory(String supply_name,String supply_category,Map<String,Object> map){
        map.put("supply_name",supply_name);
        map.put("supply_category",supply_category);
        return "changecategory";
    }

    @RequestMapping("/changecategory")
    @ResponseBody
    public int changecategory(String supply_name, String category){
        int row=ss.changecategory(supply_name,category);
        return row;
    }



    @RequestMapping("/changeexamine")
    @ResponseBody
    public int changeexamine(String supply_name,Map<String,Object> map){
        int row=ss.changeexamine(supply_name);
        //return "redirect:findsimple2";
        return row;
    }

    @RequestMapping("to_reject")
    public String to_reject(String supply_name,Map<String,Object> map){
        map.put("supply_name",supply_name);
        return "reject";
    }

    @RequestMapping("/rejectmsg")
    @ResponseBody
    public int rejectmsg(String supply_name,String reject_msg){
        int row=ss.rejectmsg(supply_name,reject_msg);
        System.out.println(reject_msg);
        System.out.println(row);
        return row;
    }


}
